﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Client.Models;
using Newtonsoft.Json;

namespace Client.Base
{
    public partial class FrmUsrMgr : Form
    {
        public FrmUsrMgr()
        {
            InitializeComponent();
        }

        private UserInfoModel rum = new UserInfoModel();
        private void FrmUsrMgr_Load(object sender, EventArgs e)
        {
            try
            {
                label2.Text = "";
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.uom.Token);
                RetModel rm = Share.PostData("", dc, null);
                if (rm.Success)
                {
                    rum = JsonConvert.DeserializeObject<UserInfoModel>(rm.Data.ToString());
                    label2.Text = rum.LoginName;
                }
                else
                {
                    MessageBox.Show("添加失败！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息：" + rm.ErrInfo);
                    return;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                string OldPass = CryptUtil.Crypt.MD32Crypt(OldPassword.Text.Trim());
                if (rum.Password != OldPass)
                {
                    MessageBox.Show("旧密码错误！请重新填写!");
                    OldPassword.Focus();
                    NewPassword1.Text = "";
                    NewPassword2.Text = "";
                    return;
                }
                else
                {
                    if (NewPassword1.Text.Trim() == NewPassword2.Text.Trim())
                    {
                        Dictionary<string, string> dc = new Dictionary<string, string>();
                        dc.Add("Token", Share.uom.Token);
                        UserInfoModel uim = new UserInfoModel();
                        if (NewPassword1.Text == NewPassword2.Text)
                            uim.Password = CryptUtil.Crypt.MD32Crypt(NewPassword1.Text.Trim());
                        RetModel rm = Share.PostData("", dc, JsonConvert.SerializeObject(uim));
                        if (rm.Success)
                        {
                            MessageBox.Show("修改密码完成!");
                            return;
                        }
                        else
                        {
                            MessageBox.Show("修改密码失败！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息：" + rm.ErrInfo);
                            return;
                        }
                    }
                    else
                    {
                        MessageBox.Show("新密码两次输入不一致，请重新输入！");
                        NewPassword1.Text = "";
                        NewPassword2.Text = "";
                        NewPassword1.Focus();
                        return;
                    }
                    

                }

                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

       
    }
}
